package com.neusoft.actor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.actor.dao.BedDao;
import com.neusoft.actor.dao.BedUsageDao;
import com.neusoft.actor.dao.ClientDao;
import com.neusoft.actor.dto.RoomDto;
import com.neusoft.actor.po.Bed;
import com.neusoft.actor.po.BedUsage;
import com.neusoft.actor.po.Client;
import com.neusoft.actor.po.ResponseBean;
import com.neusoft.actor.service.BedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedDao bedDao;
    @Autowired
    private ClientDao clientDao;
    @Autowired
    private BedUsageDao bedUsageDao;

    @Override
    public ResponseBean<List<Bed>> getAllBeds() {
        List<Bed> beds = bedDao.selectList(null);
        return new ResponseBean<>(beds);
    }

    @Override
    public ResponseBean<List<Bed>> getAllAccessBeds() {
        List<Bed> beds = bedDao.selectList(new QueryWrapper<Bed>().eq("status", "空闲"));
        return new ResponseBean<>(beds);
    }

    @Override
    public ResponseBean<Bed> getBedById(Integer bedId) {
        Bed bed = bedDao.selectById(bedId);
        if (bed != null) {
            return new ResponseBean<>(bed);
        } else {
            return new ResponseBean<>(404, "床位未找到");
        }
    }

    @Override
    public ResponseBean<Integer> addBed(Bed bed) {
        int result = bedDao.insert(bed);
        if (result > 0) {
            return new ResponseBean<>(result); // 返回插入成功的行数
        } else {
            return new ResponseBean<>(500, "床位添加失败");
        }
    }

    @Override
    public ResponseBean<Integer> updateBed(Bed bed) {
        int result = bedDao.updateById(bed);
        if (result > 0) {
            return new ResponseBean<>(result); // 返回更新成功的行数
        } else {
            return new ResponseBean<>(500, "床位信息更新失败");
        }
    }

    @Override
    public ResponseBean<Integer> deleteBed(Integer bedId) {
        int result = bedDao.deleteById(bedId);
        if (result > 0) {
            return new ResponseBean<>(result); // 返回删除成功的行数
        } else {
            return new ResponseBean<>(500, "床位删除失败");
        }
    }

    @Override
//    查询楼层所有房间
    public ResponseBean<List<RoomDto>> getAllRoomsByFloor() {
        List<RoomDto> roomDtos = bedDao.getAllRoomsByFloor();
        return new ResponseBean<>(roomDtos);
    }

    @Override
//    获取该房间内的空闲床位
    public ResponseBean<List<Bed>> getFreeBedsByRoom(String roomNumber) {
        List<Bed> beds = bedDao.selectList(new QueryWrapper<Bed>().eq("status", "空闲").eq("room_number", roomNumber));
        return new ResponseBean<>(beds);
    }

    @Override
    public ResponseBean<List<Bed>> getAllBedsByRoom(String roomNumber) {
        List<Bed> beds = bedDao.selectList(new QueryWrapper<Bed>().eq("room_number", roomNumber));
        return new ResponseBean<>(beds);
    }

    @Override
//    换床位
    public ResponseBean<Integer> changeBed(Integer oldBedId, Integer newBedId, Integer clientId) {
        BedUsage oldBedUsage = bedUsageDao.selectOne(new QueryWrapper<BedUsage>().eq("bed_id", oldBedId).eq("client_id", clientId).eq("is_current", 1));
        oldBedUsage.setIsCurrent(false);
        oldBedUsage.setEndDate(LocalDate.now());
        bedUsageDao.updateById(oldBedUsage);

        BedUsage newBedUsage = new BedUsage();
        newBedUsage.setBedId(newBedId);
        newBedUsage.setClientId(clientId);
        newBedUsage.setIsCurrent(true);
        newBedUsage.setStartDate(oldBedUsage.getEndDate());
        newBedUsage.setEndDate(oldBedUsage.getEndDate());
        bedUsageDao.insert(newBedUsage);

        Bed oldBed = bedDao.selectById(oldBedId);
        oldBed.setStatus("空闲");
        bedDao.updateById(oldBed);

        Bed newBed = bedDao.selectById(newBedId);
        newBed.setStatus("有人");
        bedDao.updateById(newBed);

        Client client = clientDao.selectById(clientId);
        client.setBedNumber(newBed.getBedNumber());
        client.setRoomNumber(newBed.getRoomNumber());
        clientDao.updateById(client);
        return new ResponseBean<>(1);
    }

    @Override
//    根据房间号和床号查床
    public ResponseBean<Bed> getBedByCon(String roomNumber, String bedNumber) {
        return new ResponseBean<>(bedDao.selectOne(new QueryWrapper<Bed>().eq("room_number", roomNumber).eq("bed_number", bedNumber)));
    }
}